package com.hilin.cloud.gateway;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.gateway.filter.FilterDefinition;
import org.springframework.cloud.gateway.route.RouteDefinition;

import java.net.URI;
import java.util.ArrayList;
import java.util.List;

@SpringBootApplication
public class GatewayApplication implements ApplicationRunner {

    @Autowired
    private DynamicRouteServiceImpl dynamicRouteService;

    public static void main(String[] args) {
        SpringApplication.run(GatewayApplication.class, args);
    }

    @Override
    public void run(ApplicationArguments args) throws Exception {
       new Thread(()->{
           try {
               Thread.sleep(1000);
           } catch (InterruptedException e) {
               e.printStackTrace();
           }
           RouteDefinition routeDefinition = new RouteDefinition();
           routeDefinition.setId("baidu");
           routeDefinition.setOrder(1);
           routeDefinition.setUri(URI.create("https://www.baidu.com"));
           FilterDefinition filter = new FilterDefinition();
           filter.setName("baidu");
           List<FilterDefinition> lists = new ArrayList<>();
           lists.add(filter);
           routeDefinition.setFilters(lists);
           dynamicRouteService.add(routeDefinition);
       }).start();
    }
}
